package com.wx.controller;

import com.bean.Message;
import com.mvc.ResponseBody;
import com.mvc.ResponseView;
import com.service.ExpressService;
import com.util.JSONUtil;
import com.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 柏竹
 * @Description: 扫码应用
 * @Date_Created_in: 2021-05-30 11:43
 * @Modified_By: Sans
 * @Project: 快递e栈
 */
public class QRCodeController {
    
    @ResponseView ("/wx/createQRCode.do")
    public String findAll(HttpServletRequest req, HttpServletResponse resp){
        String code = req.getParameter("code");
        String type = req.getParameter("type");
        String userPhone = UserUtil.getWxUser(req.getSession()).getUserphone();
        String qrCodeContent = null;
        Message msg = null;
        if ("express".equals(type)){
        //    code
            qrCodeContent = "express_"+code;
        }else{
        //    userPhone
            qrCodeContent = "userPhone_"+userPhone;
        }
        req.getSession().setAttribute("qrcode",qrCodeContent);
        return "/wx/views/personQRcode.html";
    }
    
    @ResponseBody ("/wx/qrcode.do")
    public String getQRCode(HttpServletRequest req, HttpServletResponse resp){
        String qrcode = (String) req.getSession().getAttribute("qrcode");
        Message msg = null;
        if (qrcode == null) {
            msg = new Message("出错，请重新操作",-1,null);
        }else{
            msg = new Message(qrcode,0,null);
        }
        return JSONUtil.toJSON(msg);
    }
    
    @ResponseBody ("/wx/updateStatus.do")
    public String updateStatus(HttpServletRequest req, HttpServletResponse resp){
        String code = req.getParameter("code");
        // String code = "884338";
        boolean find = ExpressService.updateStatus(code);
        Message msg = new Message();
        if (find){
            msg.setStatus(0);
            msg.setResult("取件成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("取件失败，请更新二维码");
        }
        return JSONUtil.toJSON(msg);
    }
    
}
